//
//  MoreSortPickTimeController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/12/16.
//  Copyright © 2017 蓝布鲁. All rights reserved.
//

#import "MoreSortPickTimeController.h"
#import "View+MASAdditions.h"
#import "MoreSortOrderController.h"
#import "NSDate+Formatter.h"

#define DATE_FORMAT @"yyyy-MM-dd"

@interface MoreSortPickTimeController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)UIDatePicker *datePicker;
@property (nonatomic, strong)UIToolbar *toolBar;
@property (nonatomic, strong)UIView *backView;


@property (nonatomic, assign)BOOL isSelectStartCell;

@property (nonatomic, strong)NSDate *startTime;

@property (nonatomic, strong)NSDate *endTime;

@end

@implementation MoreSortPickTimeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"查询明细";
    self.startTime = [NSDate date];

    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(backButtonTapped)];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"查询" style:UIBarButtonItemStyleDone target:self action:@selector(searchButtonTapped)];

}

- (void)showDataPicker{

    self.datePicker = [[UIDatePicker alloc] init];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    self.datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_cn"];
    self.datePicker.backgroundColor = [UIColor whiteColor];
//    [self.datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    if(self.isSelectStartCell){
        self.datePicker.minimumDate = [NSDate date];
        self.datePicker.date = self.startTime;
    } else{
        self.datePicker.minimumDate = self.startTime;
        self.datePicker.date = self.endTime ? self.endTime : self.startTime;
    }
    [self.view addSubview:self.datePicker];

    self.toolBar = [[UIToolbar alloc] init];
    self.toolBar.barStyle = UIBarStyleDefault;
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(dismissDatePicker)];
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonTapped)];
    [self.toolBar setItems:@[cancelButton, spacer, doneButton]];
    [self.view addSubview:self.toolBar];

    self.backView = [[UIView alloc] init];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker)];
    [self.backView addGestureRecognizer:tap];
    [self.view addSubview:self.backView];


    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(@0);
        make.bottom.equalTo(self.toolBar.mas_top);
    }];
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.toolBar.mas_bottom);
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view);
    }];
    [self.toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.datePicker);
        make.top.equalTo(self.view.mas_bottom);
        make.centerX.equalTo(self.view);
    }];

    [self.view layoutIfNeeded];

    [self.datePicker mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view);
    }];
    [self.toolBar mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.datePicker);
        make.bottom.equalTo(self.datePicker.mas_top);
        make.centerX.equalTo(self.view);
    }];
    [UIView animateWithDuration:0.4 animations:^{
        [self.view layoutIfNeeded];
    }];

}

#pragma mark 事件监听
- (void)backButtonTapped{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)searchButtonTapped{
    MoreSortOrderController *vc = [[MoreSortOrderController alloc] init];
    vc.startTimeString = [self.startTime stringDateFromFormat:DATE_FORMAT];
    vc.endTimeString = [self.endTime stringDateFromFormat:DATE_FORMAT];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)doneButtonTapped{
    if(self.isSelectStartCell){
        self.startTime = self.datePicker.date;
    } else{
        self.endTime = self.datePicker.date;
    }
    [self.tableView reloadData];
    [self dismissDatePicker];
}

- (void)dismissDatePicker{
    [self.datePicker mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.toolBar.mas_bottom);
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view);
    }];
    [self.toolBar mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.datePicker);
        make.top.equalTo(self.view.mas_bottom);
        make.centerX.equalTo(self.view);
    }];
    [UIView animateWithDuration:0.4 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.datePicker removeFromSuperview];
        [self.backView removeFromSuperview];
    }];
}


#pragma mark UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    if(indexPath.row == 0){
        cell.textLabel.text = @"起始时间";
        cell.detailTextLabel.text = [self.startTime stringDateFromFormat:DATE_FORMAT];
    }else{
        cell.textLabel.text = @"截止时间";
        if(self.endTime){
            cell.detailTextLabel.text = [self.endTime stringDateFromFormat:DATE_FORMAT];
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.isSelectStartCell = indexPath.row == 0;
    [self showDataPicker];
}


@end
